package com.aizhixin.lab.project.course.service;

import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.common.dto.SortDTO;
import com.aizhixin.lab.common.utils.PageJdbcUtil;
import com.aizhixin.lab.project.course.domain.ProjectMinutesCheckDomain;
import com.aizhixin.lab.project.course.domain.ProjectMinutesDomain;
import com.aizhixin.lab.project.course.domain.QueryProjectMinutesDomain;
import com.aizhixin.lab.project.course.entity.ProjectMinutes;
import com.aizhixin.lab.project.course.repository.ProjectMinutesRepository;
import com.aizhixin.lab.project.course.vo.ProjectMinutesVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

@Slf4j
@Service
@Transactional
public class ProjectMinutesService {
    @Autowired
    private ProjectMinutesRepository projectMinutesRepository;
    @Autowired
    private PageJdbcUtil pageJdbcUtil;

    RowMapper<ProjectMinutesVo> minutesVoRowMapper = new RowMapper<ProjectMinutesVo>() {

        @Override
        public ProjectMinutesVo mapRow(ResultSet rs, int rowNum) throws SQLException {

            ProjectMinutesVo domain = new ProjectMinutesVo();
            domain.setId(rs.getString("id"));
            domain.setCheckStatus(rs.getInt("check_status"));
            domain.setMinutesName(rs.getString("minutes_name"));
            domain.setMinutesType(rs.getInt("minutes_type"));
            domain.setMinutesContent(rs.getString("minutes_content"));
            domain.setCreatedBy(rs.getString("created_by"));
            domain.setCreatedDate(rs.getString("created_date"));
            domain.setCheckSuggest(rs.getString("check_suggest"));
            domain.setGiveMark(rs.getDouble("give_mark"));
            return domain;
        }
    };

    public ProjectMinutes save(ProjectMinutesDomain domain) {
        ProjectMinutes projectMinutes = new ProjectMinutes();
        BeanUtils.copyProperties(domain, projectMinutes);
        projectMinutes.setId(UUID.randomUUID().toString());
        projectMinutes.setCreatedDate(new Date());
        projectMinutes.setDeleteFlag(DataValidity.VALID.getState());
        projectMinutes.setCheckStatus(0);
        return projectMinutesRepository.save(projectMinutes);
    }

    public ProjectMinutes update(ProjectMinutesDomain domain) {
        ProjectMinutes minutes = projectMinutesRepository.findOne(domain.getId());
        BeanUtils.copyProperties(domain, minutes);
        return projectMinutesRepository.save(minutes);
    }

    public ProjectMinutes check(ProjectMinutesCheckDomain domain) {
        ProjectMinutes minutes = projectMinutesRepository.findOne(domain.getId());
        BeanUtils.copyProperties(domain, minutes);
        minutes.setCheckTime(new Date());
        minutes.setCheckStatus(1);
        return projectMinutesRepository.save(minutes);
    }

    public <T>Map<String, Object> findProjectMinutes(QueryProjectMinutesDomain domain) {
        String sql="SELECT a.id,a.minutes_name,a.give_mark,a.minutes_type,a.minutes_content,a.check_status,a.check_suggest,a.created_date,b.NAME as created_by FROM t_project_minutes a LEFT JOIN t_user b ON a.created_by = b.id where a.project_id = '"+domain.getProjectId()+"' and a.group_id = '"+domain.getGroupId()+"'";
        String count ="SELECT count(*) FROM t_project_minutes a LEFT JOIN t_user b ON a.created_by = b.id where a.project_id = '"+domain.getProjectId()+"' and a.group_id = '"+domain.getGroupId()+"' order by a.check_time desc limit 0,10";
        List<SortDTO> sort = new ArrayList<SortDTO>();
        SortDTO dto = new SortDTO();
        dto = new SortDTO();
        dto.setKey("a.check_time");
        dto.setAsc(false);
        sort.add(dto);
        return pageJdbcUtil.getPageInfor(domain.getPageSize(),domain.getPageNumber(),minutesVoRowMapper,sort,sql, count);
    }


}
